package com.ep.thirdPlatformProject.maycur.helper;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ep.thirdPlatformProject.maycur.entity.expense.MayCurExpenseBillRequestParam;
import com.ep.thirdPlatformProject.maycur.entity.expense.MayCurExpenseBillResponseDetail;
import com.ep.thirdPlatformProject.maycur.entity.expense.MayCurExpenseBillResponseMain;
import com.ep.thirdPlatformProject.maycur.enums.MayCurApiEnum;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;


import java.math.BigDecimal;
import java.util.*;

import static com.ep.thirdPlatformProject.maycur.common.Constant.*;

@Slf4j
@Component
public class MayCurReimburseHelper {
    public MayCurReimburseHelper(MayCurCommonHelper commonHelper,
                                 MayCurLoginHelper loginHelper, ThreadPoolTaskExecutor executor
    ) {
        this.commonHelper = commonHelper;
        this.loginHelper = loginHelper;
        this.executor = executor;
    }

    private final MayCurCommonHelper commonHelper;

    private final MayCurLoginHelper loginHelper;

    private final ThreadPoolTaskExecutor executor;


    /**
     * 获取报销单金额汇总
     *
     * @return {@link Map}<{@link String}, {@link BigDecimal}>
     */
    public Map<String, BigDecimal> getReimburseAmount(DateTime dateTime) {
        List<MayCurExpenseBillResponseDetail> detailList = getReimburseList(dateTime);
        return commonHelper.getCommonListAmount(detailList, "报销单", dateTime, executor);
    }


    /**
     * 获取报销单金额汇总
     *
     * @param dateTime 日期时间
     * @return {@link List}<{@link MayCurExpenseBillResponseDetail}>
     */
    public List<MayCurExpenseBillResponseDetail> getReimburseList(@NotNull DateTime dateTime) {
        try {

            String url = commonHelper.constructUrl(MayCurApiEnum.GET_REIMBURSE.getUrl());
            return commonHelper.parseResponseCollecttionToList(MayCurExpenseBillResponseMain.class, url,
                    loginHelper::login,
                    pageNo -> buildRequestParam(dateTime, pageNo)
            );
        } catch (MayCurLoginHelper.TokenAcquisitionException ex) {
            log.error("token获取异常", ex);
            throw new MayCurLoginHelper.TokenAcquisitionException("登录异常", ex);
        } catch (Exception ex) {
            log.error("获取报销单列表失败!", ex);
            throw new ReimburseAcquisitionException("获取报销单列表失败!", ex);
        }
    }

    /**
     * 构建请求参数
     *
     * @param pageNo   页面
     * @param dateTime 日期时间
     * @return {@link MayCurExpenseBillRequestParam}
     */
    private MayCurExpenseBillRequestParam buildRequestParam(@NotNull DateTime dateTime, Integer pageNo) {
        // 获取本年开始时间
        long yearStartTime = DateUtil.beginOfYear(dateTime).getTime();
        // 获取通用结束时间
        long yearEndTime =  DateUtil.date(dateTime).getTime();

        // 构建参数，获取本年数据
        MayCurExpenseBillRequestParam param = new MayCurExpenseBillRequestParam()
                .formStatus(COMPLETED)
                .pageNo(pageNo)
                .procInstEndTimeStart(yearStartTime)
                .procInstEndTimeEnd(yearEndTime);
        log.info("请求参数:{}", param);
        return param;
    }


    static class ReimburseAcquisitionException extends RuntimeException {
        public ReimburseAcquisitionException(String message) {
            super(message);
        }

        public ReimburseAcquisitionException(String message, Throwable cause) {
            super(message, cause);
        }
    }
}
